﻿function get_page_size() {
    var re = {};
    if (document.documentElement && document.documentElement.clientHeight) {
        var doc = document.documentElement;
        re.width = (doc.clientWidth > doc.scrollWidth) ? doc.clientWidth - 1 : doc.scrollWidth;
        re.height = (doc.clientHeight > doc.scrollHeight) ? doc.clientHeight : doc.scrollHeight;
    }
    else {
        var doc = document.body;
        re.width = (window.innerWidth > doc.scrollWidth) ? window.innerWidth : doc.scrollWidth;
        re.height = (window.innerHeight > doc.scrollHeight) ? window.innerHeight : doc.scrollHeight;
    }
    return re;
}

$.fn.placeholder = function () {
    var i = document.createElement('input'),
		placeholdersupport = 'placeholder' in i;
    if (!placeholdersupport) {
        var inputs = jQuery(this);
        inputs.each(function () {
            var input = jQuery(this),
				text = input.attr('placeholder'),
				pdl = 0,
				height = input.outerHeight(),
				width = input.outerWidth(),
				placeholder = jQuery('<span class="phTips">' + text + '</span>');
            try {
                pdl = input.css('padding-left').match(/\d*/i)[0] * 1;
            } catch (e) {
                pdl = 5;
            }
            placeholder.css({ 'margin-left': -(width - pdl), 'height': height, 'line-height': height + "px", 'position': 'absolute', 'color': "#bebfb9", 'font-size': "12px" });
            placeholder.click(function () {
                input.focus();
            });
            if (input.val() != "") {
                placeholder.css({ display: 'none' });
            } else {
                placeholder.css({ display: 'inline' });
            }
            placeholder.insertAfter(input);
            input.keyup(function (e) {
                if (jQuery(this).val() != "") {
                    placeholder.css({ display: 'none' });
                } else {
                    placeholder.css({ display: 'inline' });
                }
            });
        });
    }
    return this;
};

$(document).ready(function () {
    $("#mainbox").height($(window).height());

    $(":input[placeholder]").each(function () {
        $(this).placeholder();
    });

    $(window).resize(function () {
        $("#mainbox").height($(window).height());
        $(".fillparentheight").each(function () {
            var parent = $(this).parent("div");
            if (parent.hasClass("horizontalbox")) {
                $(this).height(parent.height());
            }
        });
    });

    $(".fillparentheight").each(function () {
        var parent = $(this).parent("div");
        if (parent.hasClass("horizontalbox")) {
            $(this).height(parent.height());
        }
        else {
            parent.addClass("box");
            parent.addClass("verticalbox");
        }

        //如果父元素是body而不是div
        //parent = $(this).parent("body");
        //if (parent.length = 1) {
        //    $(this).height($("body").height());
        //}
    });

    var menu = $("#selectedMenu").val();
    var index = 0;
    switch (menu) {
        case "MonitorCenter":
            index = 0;
            break;
        case "DispatchCenter":
            index = 1;
            break;
        case "ReportCenter":
            index = 2;
            break;
        case "OperationManage":
            index = 3;
            break;
        case "SystemManage":
            index = 4;
            break;
    }

    $("#topNav ul li").eq(index).addClass("selected");

});


function tableToExcel(title, tableid) {
    var content = htmlEncode($('#' + tableid)[0].outerHTML);
    var formstr = "<form id='tabletoexcelform' action='/Common/TableToExcel' method='post'><input type='hidden' name='title' value='"
        + title + "' /><input type='hidden' id='tablecontent' name='content' /></form>";

    $(formstr).insertAfter($('#' + tableid));
    $("#tabletoexcelform #tablecontent").val(content);
    $("#tabletoexcelform").submit();
    $("#tabletoexcelform").remove();

    //myajax("/Common/TableToExcel",
    //    title+","+content,
    //    function (data) {
    //        if (data.success == false) {
    //            artDialogAlert(data.message);
    //            return;
    //        }
    //    },
    //    function (data) {
    //        artDialogAlert(data.statusText);
    //    },
    //    "",
    //    true
    // );
}

function htmlEncode(value){
    return $('<div/>').text(value).html();
}

function ajaxLoad(selector, url,callback) {
    var height = $(selector).height();
    var waitingDiv = "<div id='waitingPic' style='height:" + height + "px;line-height:" + height + "px;width:100%;text-align:center;position:relative;'><img src='/Images/loading.gif' style='top:50%;left:50%;position:absolute;margin-top:-50px;margin-left:-50px;'/></div>";
    $(selector).html(waitingDiv);
    $(selector).load(url, function (response, status) {
        if (status == "success") {
            $(selector).children("#waitingPic").remove();
            $(selector).hide();
            $(selector).show();
        }
        else {
            //$(selector).children("#waitingPic").html("加载失败");
            $(selector).children("#waitingPic").children("img").attr("src", "/Images/loaderror.png");
        }
        if (callback!=null) {
            callback();
        }
    });
}